package com.apache.uct.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.uct.common.entity.DataRight;
import com.apache.uct.manager.DataRightManager;

public class DataRightManagerImpl implements DataRightManager {

	protected IDao dataRightDao;
	protected final String entityName = "com.apache.uct.common.entity.DataRight";

	public void setDataRightDao(IDao dataRightDao) {
		this.dataRightDao = dataRightDao;
	}

	public String saveInfo(ParamsVo<DataRight> vo) throws BusinessException {
		DataRight dataRight = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="dataRight_"+infoId;
		dataRight.setDataId(infoId);
		MethodParam param = new MethodParam("DataRight", cacheKey, "", entityName);
		param.setVaule(dataRight);
		if (dataRightDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<DataRight> vo) throws BusinessException {
		DataRight dataRight = vo.getObj();
		if (Validator.isNotNull(dataRight.getDataId())) {
			String cacheKey = "";
			//String cacheKey="dataRight_"+dataRight.getDataId();
			MethodParam param = new MethodParam("DataRight", cacheKey, "", entityName);
			param.setVaule(dataRight);
			return dataRightDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<DataRight> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		DataRight dataRight = vo.getObj();
		if (Validator.isNotNull(infoId)) {
			String cacheKey = "";
			//String cacheKey="dataRight_"+infoId;
			String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			param.setInfoId(infoId);
			DataRight info = (DataRight) dataRightDao.selectById(param);
			if (Validator.isEmpty(info)) {
				return false;
			}
			param.setVaule(info);//此处需要先将状态值赋值为删除状态
			if ("false".equals(mark)) {//逻辑删除
				param.setKey("DataRight");
				return dataRightDao.edit(param);
			} else {
				param.setParams("dataId", infoId);
				param.setDelete(true);
				return dataRightDao.delete(param);
			}
		} else if (!Validator.isEmpty(dataRight)) {
			MethodParam param = new MethodParam("ByObjeIdAndUserId", "", dataRightDao.getSql(3), entityName);
			if (Validator.isNotNull(dataRight.getUserId())) {
				param.setSqlStr(dataRightDao.getSql(4));
				param.setParams("userId", dataRight.getUserId());
			}
			param.setParams("dataKey", dataRight.getDataKey());
			param.setParams("dataName", dataRight.getDataName());
			param.setDelete(true);
			return dataRightDao.delete(param);
		}
		return false;
	}

	public Object getInfoById(ParamsVo<DataRight> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="dataRight_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return dataRightDao.selectById(param);
	}

	public Object execute(ParamsVo<DataRight> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<DataRight> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = dataRightDao.pageSelect(param);
		return page;
	}

	public List<DataRight> getList(ParamsVo<DataRight> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return dataRightDao.select(param);
	}

	public long countInfo(ParamsVo<DataRight> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return dataRightDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<DataRight> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(dataRightDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		DataRight dataRight = vo.getObj();
		if (Validator.isNotNull(dataRight.getUserId())) {
			sb.append(" and userId =:userId");
			param.setParams("userId", dataRight.getUserId());
		}
		if (Validator.isNotNull(dataRight.getUserEname())) {
			sb.append(" and userEname =:userEname");
			param.setParams("userEname", dataRight.getUserEname());
		}
		if (Validator.isNotNull(dataRight.getDataName())) {
			sb.append(" and dataName =:dataName");
			param.setParams("dataName", dataRight.getDataName());
		}
		if (Validator.isNotNull(dataRight.getDataKey())) {
			sb.append(" and dataKey =:dataKey");
			param.setParams("dataKey", dataRight.getDataKey());
		}
		if (Validator.isNotNull(dataRight.getDataValue())) {
			sb.append(" and dataValue =:dataValue");
			param.setParams("dataValue", dataRight.getDataValue());
		}
		if (Validator.isNotNull(dataRight.getDataRemark())) {
			sb.append(" and dataRemark =:dataRemark");
			param.setParams("dataRemark", dataRight.getDataRemark());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
